//
//  FWIndicatorSegmentCotrol.m
//  iOSToolkit
//
//  Created by Evan Xie on 5/27/15.
//  Copyright (c) 2015 Family Wings. All rights reserved.
//

#import "FWIndicatorSegmentCotrol.h"
#import "FWIconTextButton.h"


@interface FWIndicatorSegmentCotrol ()
{
    NSUInteger _numberOfSegments;
    
    NSMutableArray *_segmentButtons;
    
    UIImageView *_indicatorImageView;
    
    UIColor *_normalTextColor;
    UIColor *_selectedTextColor;
}

@end


@implementation FWIndicatorSegmentCotrol

#pragma mark - Object Lifecycle

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame cornerRadius:0.0];
}

- (instancetype)initWithFrame:(CGRect)frame cornerRadius:(CGFloat)cornerRadius
{
    if (self = [super initWithFrame:frame]) {
        self.cornerRadius = cornerRadius;
        _segmentButtons = [NSMutableArray arrayWithCapacity:0];
        
        _indicatorImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        _indicatorImageView.contentMode = UIViewContentModeCenter;
        _indicatorImageView.cornerRadius = cornerRadius;
        [self addSubview:_indicatorImageView];
        
        [self setIndicatorColor:[UIColor orangeColor] indicatorImage:nil];
        [self setNormalTextColor:[UIColor darkTextColor] selectedTextColor:[UIColor blueColor]];
    }
    return self;
}

- (void)dealloc
{
    _delegate = nil;
    _dataSource = nil;
    [_segmentButtons removeAllObjects];
}


#pragma mark - Object Control

- (void)setDataSource:(id<FWIndicatorSegmentCotrolDataSource>)dataSource
{
    if (_dataSource != dataSource) {
        _dataSource = dataSource;
        
        [_segmentButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [obj removeFromSuperview];
        }];
        
        [_segmentButtons removeAllObjects];
        _numberOfSegments = [_dataSource numberOfSegments];
        
        for (NSUInteger index = 0; index < _numberOfSegments; index++) {
            UIImage *image = [_dataSource indicatorSegmentControl:self iconForSegmentAtIndex:index];
            NSString *text = [_dataSource indicatorSegmentControl:self textForSegmentAtIndex:index];
            
            CGFloat width = self.frame.size.width;
            CGFloat height = self.frame.size.height;
            CGFloat segmentWidth = width / _numberOfSegments;
            
            FWIconTextButton *button = [[FWIconTextButton alloc] initWithIcon:image andText:text frame:CGRectMake(index * segmentWidth, 0, segmentWidth, height)];
            button.cornerRadius = height / 2.0;
            [button addTarget:self action:@selector(handleSegmentSelectedEvent:) forControlEvents:UIControlEventTouchUpInside];
            [_segmentButtons addObject:button];
            [self addSubview:button];
        }
        
        self.selectedIndex = 0;
        
        [self setNeedsLayout];
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if (!_dataSource) {
        return;
    }
    
    if (selectedIndex > _numberOfSegments - 1) {
        selectedIndex = _numberOfSegments - 1;
    }
    
    _selectedIndex = selectedIndex;
    [self moveIndicatorToIndex:selectedIndex];
    
    for (NSUInteger index = 0; index < _numberOfSegments; index++) {
        FWIconTextButton *button = _segmentButtons[index];
        if (index == selectedIndex) {
            [button setTextColor:_selectedTextColor textFont:nil];
        } else {
            [button setTextColor:_normalTextColor textFont:nil];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    CGFloat segmentWidth = width / _numberOfSegments;
    
    CGSize indicatorSize = _indicatorImageView.frame.size;
    CGSize newIndicatorSize = CGSizeMake(segmentWidth, height);
    if (!CGSizeEqualToSize(indicatorSize, newIndicatorSize)) {
        CGRect newFrame = _indicatorImageView.frame;
        newFrame.size = newIndicatorSize;
        _indicatorImageView.frame = newFrame;
        
        FWIconTextButton *button = _segmentButtons[_selectedIndex];
        _indicatorImageView.center = button.center;
    }
    
    for (NSUInteger index = 0; index < _numberOfSegments; index++) {
        FWIconTextButton *button = _segmentButtons[index];
        button.frame = CGRectMake(index * segmentWidth, 0, segmentWidth, height);
    }
}


#pragma mark - Appearance 

- (void)setIndicatorColor:(UIColor *)indicatorColor indicatorImage:(UIImage *)indicatorImage
{
    _indicatorImageView.backgroundColor = indicatorColor;
    _indicatorImageView.image = indicatorImage;
}

- (void)setNormalTextColor:(UIColor *)normalTextColor selectedTextColor:(UIColor *)selectedTextColor
{
    _normalTextColor = normalTextColor;
    _selectedTextColor = selectedTextColor;
}


#pragma mark - Internal Methods

- (void)handleSegmentSelectedEvent:(id)sender
{
    FWIconTextButton *button = (FWIconTextButton *)sender;
    NSUInteger index = [_segmentButtons indexOfObject:button];
    self.selectedIndex = index;
    
    if ([self.delegate respondsToSelector:@selector(indicatorSegmentControl:didSelectIndex:)]) {
        [self.delegate indicatorSegmentControl:self didSelectIndex:index];
    }
}

- (void)moveIndicatorToIndex:(NSUInteger)index {
    FWIconTextButton *button = _segmentButtons[index];
    [UIView animateWithDuration:0.15 animations:^{
        _indicatorImageView.center = button.center;
    }];
}

@end
